﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Globalization;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.Utils;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PS_RetirementYear : System.Web.UI.Page
    {
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private RadComboBox cmbEmployeeType, cmbFromYear, cmbToYear;
        Main main = new Main();
        protected void Page_Load(object sender, EventArgs e)
        {
            cmbEmployeeType = (RadComboBox)cbEmployeeType.FindControl("cbEmployeeType");
            cmbFromYear = (RadComboBox)cbFromYear.FindControl("cbYear");
            cmbToYear = (RadComboBox)cbToYear.FindControl("cbYear");

             if (!IsPostBack)
            {                
                cbEmployeeType.ListEmployeeTypeComboBox();
                cbFromYear.ListYear();
                cbToYear.ListYear();
                cmbFromYear.SelectedValue = Convert.ToString(Int32.Parse(DateTime.Today.Year.ToString()) + 543);
                cmbToYear.SelectedValue = Convert.ToString(Int32.Parse(DateTime.Today.Year.ToString()) + 543);
                LoadDataGrid();

                lblHeader.Text = "รายชื่อบุคลากรที่เกษียณในปีงบประมาณ " + cmbFromYear.SelectedValue + 
                    (!string.IsNullOrEmpty(cmbToYear.SelectedValue) ? " ถึง " + cmbToYear.SelectedValue : string.Empty);
            }
        }
        private void LoadDataGrid()
        {
            string EmpType = cmbEmployeeType.SelectedValue;
            string FromYear = cmbFromYear.SelectedValue;
            string ToYear = cmbToYear.SelectedValue;

            IEnumerable<EmployeeInfo> infoList = empBLL.ListEmployeeByTypes(EmpType, "", FromYear, ToYear).OrderBy(o=>o.RetireYear);
            
            RadGrid1.DataSource = infoList;
            //GridGroupByExpression g = new GridGroupByExpression(RadGrid1.Columns[7]);
            //RadGrid1.MasterTableView.GroupByExpressions.Add(g);
            RadGrid1.DataBind();
        }      

        protected void cmdFilter_Click(object sender, EventArgs e)
        {
            int nYearStart = Convert.ToInt32(cmbFromYear.SelectedValue);
            int nYearEnd = Convert.ToInt32(cmbToYear.SelectedValue);
            if (nYearStart > nYearEnd)
            {
                main.MessageAlert(this.Page, "เลือกช่วงปีไม่ถูกต้อง!!");
                return;
            }
            LoadDataGrid();
            lblHeader.Text = "รายชื่อบุคลากรที่เกษียณในปีงบประมาณ " + cmbFromYear.SelectedValue + 
                    (!string.IsNullOrEmpty(cmbToYear.SelectedValue) ? " ถึง " + cmbToYear.SelectedValue : string.Empty);
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadDataGrid();
        }  

    }
}
